package com.cookpan001.loader 
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	/**
	 * 加载器
	 * @author cookpan001
	 */
	public class GameLoader extends Object
	{
		private static var INSTANCE:GameLoader;
		private static var LOADERINFO_POOL:Object = {};	
		private var _swfLoader:Loader;
		private var _urlLoader:URLLoader;
		public function GameLoader() 
		{
			this.init();
		}
		
		public static function get instance():GameLoader
		{
			if (null == INSTANCE)
			{
				INSTANCE = new GameLoader();
			}
			return INSTANCE;
		}
		
		private function init():void
		{
			this._swfLoader = new Loader();
			this._urlLoader = new URLLoader();
			this._swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.loadCompleteHandler,false,0,true);
            this._swfLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.ioError,false,0,true);
            this._swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, this.loaderProgress,false,0,true);
            this._urlLoader.addEventListener(Event.COMPLETE, this.loadCompleteHandler,false,0,true);
            this._urlLoader.addEventListener(IOErrorEvent.IO_ERROR, this.ioError,false,0,true);
            this._urlLoader.addEventListener(ProgressEvent.PROGRESS, this.loaderProgress,false,0,true);
		}
		
		protected function loadCompleteHandler(event:Event) : void
        {
			
		}
		
		protected function ioError(event:IOErrorEvent) : void
        {
			
		}
		
		protected function loaderProgress(event:ProgressEvent) : void
        {
			
		}
		
		public function load(type:String):void
		{
			
		}
		
		/**
		 * 标识为loaderinfoKey的LoaerInfo是否存在
		 */
		public static function hasLoaderInfo(loaderinfoKey:String):Boolean
		{
			if(LOADERINFO_POOL.hasOwnProperty(loaderinfoKey))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		/**
		 * 获取Class定义
		 */
		public static function getClass(cname:String , loaderinfoKey:String):Class
		{
			try
			{	
				if(GameLoader.hasLoaderInfo(loaderinfoKey))
				{
					return LOADERINFO_POOL[loaderinfoKey].applicationDomain.getDefinition(cname) as Class;
				}
			}
			catch (e:ReferenceError)
			{
				return null;
			}
			return null;
		}
	}

}